<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
//require_once(APPPATH. 'controllers/admin/ad_layout.php');

class Ad_login extends CI_Controller 
{
	public function __construct()
	{
		parent::__construct();
		$this->load->library('form_validation');
		$this->load->model('users_model', 'users_model');
	}
	
	public function index()
	{
		$this->load->view('admin/ad_login_view');
	}
	
	public function VerifyLogin()
	{
		$this->form_validation->set_rules('username', 'Tên đăng nhập', 'trim|required|xss_clean');
		$this->form_validation->set_rules('password', 'Mật khẩu', 'trim|required|xss_clean|callback_check_database');
		$this->form_validation->set_message('required', '%s không được để trống !');
		if($this->form_validation->run() == FALSE){
			//Field validation failed.&nbsp; User redirected to login page
			$this->load->view('admin/ad_login_view');
		} else {
			//Go to private area
			redirect('admin/san-pham', 'refresh');
		}
	}
	
	public function check_database()
	{
		$username = $this->input->post('username');
		$password = md5($this->input->post('password'));
		
		$result = $this->users_model->login($username, $password);
		if(!$result){
			$this->form_validation->set_message('check_database', 'Tên đăng nhập hoặc mật khẩu không đúng');
			return false;
		}else {
			$sess_array = array();
			foreach($result as $row){
				$user_info = $this->users_model->getUserInfo($row->user_id);
				// khoi tao session
				$this->session->set_userdata('logged_in', array(
					'user_id' => $user_info->user_id,
					'full_name' => $user_info->full_name,
					'group_user' => $user_info->group_user,
					'user_name' => $user_info->user_name
				));
			}
			return true;
		} 
	}


	public function logout()
	{
		$this->session->unset_userdata('logged_in');
		$this->session->sess_destroy();
		redirect('admin/dang-nhap', 'refresh');
	}
	
	
}

